import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:miuu_note_app/dao/diary_dao.dart';
import 'package:miuu_note_app/page/diary_add_update.dart';

import '../model/diary_entity.dart';

class DiaryDetail extends StatefulWidget {
  const DiaryDetail({super.key});

  @override
  State<DiaryDetail> createState() => _DiaryDetailState();
}

class _DiaryDetailState extends State<DiaryDetail> {
  DiaryModel? _diaryModel;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      dynamic arguments = ModalRoute.of(context)?.settings.arguments;
      if (arguments != null) {
        _getDiaryDetail(arguments['diaryId']);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            appBar: BrnAppBar(
              leading: BrnBackLeading(
                iconPressed: () => Navigator.pop(context),
              ),
              //默认显示返回按钮
              automaticallyImplyLeading: true,
              title: '日日记',
            ),
            body: SingleChildScrollView(
                child: Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                  Padding(
                      padding: const EdgeInsets.only(
                          left: 25, right: 25, top: 25, bottom: 20),
                      child: GestureDetector(
                        child: Text(_diaryModel?.title ?? '暂无标题',
                            textAlign: TextAlign.left,
                            style: const TextStyle(
                                fontSize: 20, fontWeight: FontWeight.w600)),
                        onTap: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: (context) => const DiaryAdd(),
                                settings: RouteSettings(arguments: {
                                  "diary": _diaryModel
                                }) // 需要跳转的页面
                                ),
                          ).then((value) {
                            if (value != null) {
                              _getDiaryDetail(int.parse(value.data));
                            }
                          });
                        },
                      )),
                  Padding(
                    padding: const EdgeInsets.only(
                        left: 25, right: 25, top: 0, bottom: 20),
                    child: Text(_diaryModel?.content ?? '',
                        textAlign: TextAlign.justify,
                        style: const TextStyle(fontSize: 14)),
                  ),
                ]))));
  }

  void _getDiaryDetail(int diaryId) async {
    DiaryEntity? entity = await DiaryDetailDao.fetch(diaryId);
    setState(() {
      _diaryModel = entity?.diaryModel;
    });
  }
}
